<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class Role extends AdminController
{
    public function index()
    {
        $list = db('ad_user')->field(['id','name','status'])->order('id','ASC')->select();

        return view('index/index', [
            'list'=>$list
        ]);
    }

    public function role()
    {
        $list = db('ad_role')->field(['id','name'])->order('id','ASC')->select();

        return view('role/index', [
            'title' => '角色表','list'=>$list
        ]);
    }

    public function main()
    {
        $list = db('ad_user')->field(['id','name','status'])->order('id','ASC')->select();

        return view('main/index', [
            'title' => '登陆页','list'=>$list,'time'=>date('Y/m/d',time())
        ]);
    }

    //确认注册
    public function register(Request $request)
    {
        $p = $request->post();
//        var_dump($p)
        $data = [
            'name' => $p['name'],
            'pwd' => ($p['pwd'])
        ];
        $res = db('ad_user')->data($data)->insert();
        if ($res) {
//            return $this->success('注册成功', 'admin/index/login');
            return $this->redirect('admin/login/login');
        } else {
            return $this->error('注册失败');
        }
    }

    //注册页
    public function reg()
    {
        return view('login/register', [
            'title' => '注册页'
        ]);
    }

    //确认登陆
    public function dologin(Request $request)
    {
        $p = $request->post();
        $data = [
            'name' => $p['name'],
            'pwd' => ($p['pwd']),
        ];

        $res = db('ad_user')
            ->where(['name'=>$data['name'],'pwd'=>$data['pwd']])
            ->find();
       // var_dump($res);die;
        if ($res){
//            return $this->success('登陆成功', 'login/index/show');
            return view('main/index',[
                'title'=>$data['name']
            ]);
        }else {
            return $this->error('登陆失败');
        }
    }

    public function edit($id)
    {
        $list = Db::table('mh_ad_role')->where('id',$id)->select();
        return view('role/edit',[
            'list'=>$list[0],
        ]);
    }

    public function create()
    {
        return view('role/add');
        
        
        $name = $_POST['name'];
        
        
        $data = [
            'name' => $name
        ];
        $result = db('ad_role')->insert($data);
        if($result){
        $this->success('添加成功');
        }
}

    public function save(Request $request)
    {
        $result = db('ad_role')->insert($_POST);
        if($result){
        $this->success('添加成功','admin/role/role');
        }

    }

    public function update(Request $request, $id)
    {

        $id = $_POST['id'];
        $name = $_POST['name'];
        
        $result = db('ad_role')->where('id', $id)->update(['name' => $name]);
        
        if($result){
        $this->success('修改成功','admin/role/role');
     }
    }

    public function delete($id)
    {
        $result = Db::name('ad_role')->delete($id);
        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的用户删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的用户删除失败!';
        }
        // JSON 返回
        return json($info);
    
    }

    public function accmanage($id){
        $access_id = db('ad_role_access')->where('role_id',$id)->field('access_id')->select();

        // var_dump($access_id);die;
        //查找所有的角色
        $data = db('ad_access')->select();

        $roid = array();
       //遍历重组数组
        foreach($access_id as $v){
            $roid[] = $v['access_id'];
        }
        // var_dump($roid);die;
//        dump($ro_nodes);die;
        return view('main/roleaccess',[
            'roid'=>$roid,
            // 'guanli' => $guanli,
            'data' => $data,
            'access_id' => $access_id,
            'uid' => $id
        ]);
    }

    public function rolesave(Request $request)
    {
        $arr = array();
        $p = $request->post();
//        dump($p);die;
        $data = $p['a'];
//        dump($data);die;
        $user_id = $p['uid'];

        $qu = db('ad_role_access')->field('role_id')->select();
//        dump($qu);die;
        foreach ($qu as $k=>$v){
           if (in_array($user_id,$v)){
               $res = db('ad_role_access')->where('role_id',$user_id)->delete();
           }
        }

        foreach($data as $v){
            $arr['role_id'] = $user_id;
            $arr['access_id'] = $v;
            $rev = db('ad_role_access')->data($arr)->insert();
            $arr = [];
        }
        if ($rev > 0){
            return $this->success('编辑成功', url('admin/role/index'));
        }else{
            return $this->error('编辑失败');
        }
    }

}
